package org.tsinghua.air;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.tsinghua.air.common.Constant.Database;
import org.tsinghua.air.db.DbConnection;
import org.tsinghua.air.ui.BackgroudComposite;
import org.tsinghua.air.ui.TableComposite;
import org.tsinghua.air.xls.XlsHandler;

public class MainApp {

	protected Shell shell;
	// private final FormToolkit formToolkit = new
	// FormToolkit(Display.getDefault());
	private Composite contentPanel;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			MainApp window = new MainApp();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.setMaximized(true);
		shell.open();
		shell.layout();
		shell.addListener(SWT.Close, new Listener() {

			public void handleEvent(Event ev) {
				boolean result = MessageDialog.openConfirm(shell, "Confirm", "Are you sure you want to quit?");
				if (result == true) {
					DbConnection.closeConnection();
				}
				ev.doit = result;
			}
		});
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setImage(SWTResourceManager.getImage(MainApp.class, "/icons/progress/ani/5.png"));
		shell.setMinimumSize(new Point(680, 460));
		shell.setSize(720, 500);
		shell.setText(" \u6E05\u534E\u5927\u5B66");

		showMenu();

		contentPanel = new BackgroudComposite(shell, SWT.BORDER);
	}

	private void showMenu() {
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		Menu menu = new Menu(shell, SWT.BAR);
		shell.setMenuBar(menu);

		MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
		mntmFile.setText("File");

		Menu menu_1 = new Menu(mntmFile);
		mntmFile.setMenu(menu_1);

		MenuItem mntmImport = new MenuItem(menu_1, SWT.NONE);
		mntmImport.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(shell, SWT.OPEN);
				fd.setText("Open");
//				fd.setFilterPath("C:/");
				fd.setFilterExtensions(new String[] { "*.xls; *.xlsx", "*.*"});
				String selected = fd.open();
				try {
					XlsHandler.readFile(selected);
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		mntmImport.setText("Import");

		MenuItem mntmExport = new MenuItem(menu_1, SWT.NONE);
		mntmExport.setImage(SWTResourceManager.getImage(MainApp.class, "/com/sun/java/swing/plaf/windows/icons/FloppyDrive.gif"));
		mntmExport.setText("Export");

		new MenuItem(menu_1, SWT.SEPARATOR);

		MenuItem mntmExit = new MenuItem(menu_1, SWT.NONE);
		mntmExit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		mntmExit.setText("Exit");

		MenuItem mntmData = new MenuItem(menu, SWT.CASCADE);
		mntmData.setText("Data");

		Menu menu_2 = new Menu(mntmData);
		mntmData.setMenu(menu_2);

		MenuItem mntmSearchbrowse = new MenuItem(menu_2, SWT.NONE);
		mntmSearchbrowse.setText("Search / browse");

		MenuItem mntmMaintain = new MenuItem(menu_2, SWT.CASCADE);
		mntmMaintain.setText("Maintain Tables");

		Menu menu_5 = new Menu(mntmMaintain);
		mntmMaintain.setMenu(menu_5);

		final MenuItem mntmCountry = new MenuItem(menu_5, SWT.NONE);
		mntmCountry.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}

		});
		mntmCountry.setText(Database.COUNTRY.toString());

		MenuItem mntmDevelopment = new MenuItem(menu_5, SWT.NONE);
		mntmDevelopment.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmDevelopment.setText(Database.DEVELOPMENT_STAGE.toString());

		new MenuItem(menu_5, SWT.SEPARATOR);

		MenuItem mntmIndustrializationIndex = new MenuItem(menu_5, SWT.NONE);
		mntmIndustrializationIndex.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmIndustrializationIndex.setText(Database.INDUSTRIALIZATION_INDEX.toString());

		MenuItem mntmLawPolicy = new MenuItem(menu_5, SWT.NONE);
		mntmLawPolicy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmLawPolicy.setText(Database.LAW_POLICY.toString());

		MenuItem mntmRuleOfLaw = new MenuItem(menu_5, SWT.NONE);
		mntmRuleOfLaw.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmRuleOfLaw.setText(Database.RULE_OF_LAW_INDEX.toString());

		MenuItem mntmPeremission = new MenuItem(menu_5, SWT.NONE);
		mntmPeremission.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmPeremission.setText(Database.PER_EMISSION.toString());

		MenuItem mntmPergdp = new MenuItem(menu_5, SWT.NONE);
		mntmPergdp.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmPergdp.setText(Database.PER_GDP.toString());

		MenuItem mntmUrban = new MenuItem(menu_5, SWT.NONE);
		mntmUrban.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				showTable(((MenuItem) e.getSource()).getText());
			}
		});
		mntmUrban.setText(Database.URBAN.toString());

		new MenuItem(menu_2, SWT.SEPARATOR);

		MenuItem mntmBackup = new MenuItem(menu_2, SWT.NONE);
		mntmBackup.setImage(SWTResourceManager.getImage(MainApp.class, "/com/sun/java/swing/plaf/windows/icons/HardDrive.gif"));
		mntmBackup.setText("Backup");

		MenuItem mntmCalstat = new MenuItem(menu, SWT.CASCADE);
		mntmCalstat.setText("Calculate/Statistics");

		Menu menu_4 = new Menu(mntmCalstat);
		mntmCalstat.setMenu(menu_4);

		MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
		mntmHelp.setText("Help");

		Menu menu_3 = new Menu(mntmHelp);
		mntmHelp.setMenu(menu_3);

		MenuItem mntmHelpContent = new MenuItem(menu_3, SWT.NONE);
		mntmHelpContent.setText("&Help Contents");

		new MenuItem(menu_3, SWT.SEPARATOR);

		MenuItem mntmAbout = new MenuItem(menu_3, SWT.NONE);
		mntmAbout.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				changeContent(new BackgroudComposite(shell, SWT.NONE));
			}
		});
		mntmAbout.setText("About");
	}

	private void showTable(String db) {
		TableComposite tc;
		if (contentPanel instanceof TableComposite) {
			tc = (TableComposite) contentPanel;
		} else {
			tc = new TableComposite(shell, SWT.NONE);
			changeContent(tc);
		}
		tc.selectTable(db);
	}

	protected void changeContent(Composite compos) {
		contentPanel.dispose();
		contentPanel = compos;
		shell.layout();
	}
}
